<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.dao.IMutiTableDao">

	<#list tablesQueryMap?keys as key>
	
		<select id="${key}Select" parameterType="java.util.HashMap"
		resultType="java.util.LinkedHashMap">
		
		<#assign formSql = tablesQueryMap["${key}"].formSql/>
		<#assign tableFiledModels = tablesQueryMap["${key}"].tableFiledModels/>
		<#assign tableConditionModels = tablesQueryMap["${key}"].tableConditionModels/>
		
		<#if dataBaseType == "mysql">
			select
			
			<#list tableFiledModels as data>
				${data.anotherTableName}.${data.filedText_eng} ${data.anotherFiledName}<#if data_has_next>,</#if>
			</#list>
			
			${formSql}
			<where>
				<#list tableConditionModels as data>
					<#if data.compareText==">= && <=">
						<if test="start${data.anotherTableName}_${data.filed_eng} != null and start${data.anotherTableName}_${data.filed_eng} != '' ">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
						<if test="end${data.anotherTableName}_${data.filed_eng} != null and end${data.anotherTableName}_${data.filed_eng} != '' ">
						and ${data.anotherTableName}.${data.filed_eng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
					<#else>
						<#if data.unchangeValue!="">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> '${data.unchangeValue}'
						<#else>
						<if test="${data.anotherTableName}_${data.filed_eng} != null and ${data.anotherTableName}_${data.filed_eng} != '' ">
						<#if data.compareText=="like">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} like <#noparse>"%"#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}"%"</#noparse>
						<#else>
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> <#noparse>#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</#if>
						</if>
						</#if>
					</#if>
				</#list>
			</where>
			<if test="orderStr != '' and orderStr != null">
				order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
			</if>
			<if test="start != null and pageSize != null">
				limit <#noparse>#{</#noparse>start<#noparse>}</#noparse>,<#noparse>#{</#noparse>pageSize<#noparse>}</#noparse>
			</if>
		</#if>
		
		<#if dataBaseType == "oracle">
			SELECT
			<#list tableFiledModels as data>
				${data.anotherTableName}.${data.filedText_eng} ${data.anotherFiledName}<#if data_has_next>,</#if>
			</#list>
			
			<if test="startIndex == null or endIndex == null">
			${formSql}
	                  <where>
						<#list tableConditionModels as data>
						<#if data.compareText==">= && <=">
						<if test="start${data.anotherTableName}_${data.filed_eng} != null and start${data.anotherTableName}_${data.filed_eng} != '' ">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
						<if test="end${data.anotherTableName}_${data.filed_eng} != null and end${data.anotherTableName}_${data.filed_eng} != '' ">
						and ${data.anotherTableName}.${data.filed_eng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
						<#else>
						<#if data.unchangeValue!="">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> '${data.unchangeValue}'
						<#else>
						<if test="${data.anotherTableName}_${data.filed_eng} != null and ${data.anotherTableName}_${data.filed_eng} != '' ">
						<#if data.compareText=="like">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} like <#noparse>"%"#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}"%"</#noparse>
						<#else>
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> <#noparse>#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</#if>
						</if>
						</#if>
						</#if>
						</#list>
					  </where>
					  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
			</if>
			<if test="startIndex != null and endIndex != null">
			
	  			FROM (SELECT tb2.*, ROWNUM rowno
	
	          	FROM (SELECT tb1.*
	
	                  FROM (SELECT 
	                 <#list tableFiledModels as data>
					  ${data.anotherTableName}.${data.filedText_eng} ${data.anotherFiledName}<#if data_has_next>,</#if>
					 </#list>
	                  ${formSql}) tb1
	
	                  <where>
		
						<#list tableConditionModels as data>
						<#if data.compareText==">= && <=">
						<if test="start${data.anotherTableName}_${data.filed_eng} != null and start${data.anotherTableName}_${data.filed_eng} != '' ">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
						<if test="end${data.anotherTableName}_${data.filed_eng} != null and end${data.anotherTableName}_${data.filed_eng} != '' ">
						and ${data.anotherTableName}.${data.filed_eng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</if>
						<#else>
						<#if data.unchangeValue!="">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> '${data.unchangeValue}'
						<#else>
						<if test="${data.anotherTableName}_${data.filed_eng} != null and ${data.anotherTableName}_${data.filed_eng} != '' ">
						<#if data.compareText=="like">
						${data.relation} ${data.anotherTableName}.${data.filed_eng} like <#noparse>"%"#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}"%"</#noparse>
						<#else>
						${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> <#noparse>#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
						</#if>
						</if>
						</#if>
						</#if>
						</#list>
						
					  </where>
	
	                  <if test="orderStr != '' and orderStr != null">
						order by <#noparse>${</#noparse>orderStr<#noparse>}</#noparse>
					  </if>
	                  
	                  ) tb2
	
	           <![CDATA[WHERE ROWNUM <= <#noparse>#{</#noparse>endIndex<#noparse>}</#noparse>) tb3
	
	 		   WHERE tb3.rowno >= <#noparse>#{</#noparse>startIndex<#noparse>}</#noparse>]]>
	 		</if>
		</#if>
		
	</select>
		
	<select id="${key}SelectCount" parameterType="java.util.HashMap"
		resultType="java.lang.Long">

		SELECT
		count(1)
		${formSql}
		<where>
			<#list tableConditionModels as data>
			<#if data.compareText==">= && <=">
			<if test="start${data.anotherTableName}_${data.filed_eng} != null and start${data.anotherTableName}_${data.filed_eng} != '' ">
			${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[>=]]> <#noparse>#{</#noparse>start${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
			</if>
			<if test="end${data.anotherTableName}_${data.filed_eng} != null and end${data.anotherTableName}_${data.filed_eng} != '' ">
			and ${data.anotherTableName}.${data.filed_eng} <![CDATA[<=]]> <#noparse>#{</#noparse>end${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
			</if>
			<#else>
			<#if data.unchangeValue!="">
			${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> '${data.unchangeValue}'
			<#else>
			<if test="${data.anotherTableName}_${data.filed_eng} != null and ${data.anotherTableName}_${data.filed_eng} != '' ">
			<#if data.compareText=="like">
			${data.relation} ${data.anotherTableName}.${data.filed_eng} like <#noparse>"%"#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}"%"</#noparse>
			<#else>
			${data.relation} ${data.anotherTableName}.${data.filed_eng} <![CDATA[${data.compareText}]]> <#noparse>#{</#noparse>${data.anotherTableName}_${data.filed_eng}<#noparse>}</#noparse>
			</#if>
			</if>
			</#if>
			</#if>
			</#list>
		</where>
	</select>
	
	</#list>
</mapper>